﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Subattle
{
    public class BubbleParticles
    {
        /// <summary>
        /// The canvas on which to draw particles
        /// </summary>
        Canvas parentCanvas;

        /// <summary>
        /// The Silverlight Ellipse objects that represent our bubbles
        /// </summary>
        Ellipse[] bubbles;
        
        /// <summary>
        /// Each bubble has it's own trajectory.
        /// </summary>
        Vector2[] bubbleMovement;

        /// <summary>
        /// We need some random numbers for spawning bubbles
        /// </summary>
        Random randomizer = new Random();

        /// <summary>
        /// Initialize a new bubble particle system
        /// </summary>
        /// <param name="maxParticles">The maximum allowed amount of bubbles on the screen at any time</param>
        /// <param name="parentCanvas">The canvas on which to draw the particles</param>
        public BubbleParticles(int maxParticles, Canvas parentCanvas)
        {
            this.parentCanvas = parentCanvas;
            bubbleMovement = new Vector2[maxParticles];
            bubbles = new Ellipse[maxParticles];

            for (int i = 0; i < maxParticles; i++)
            {
                bubbles[i] = new Ellipse();
                bubbles[i].Visibility = Visibility.Collapsed;
                bubbles[i].Stroke = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));
                parentCanvas.Children.Insert(0, bubbles[i]);
            }
        }

        /// <summary>
        /// Spawns a bubble
        /// </summary>
        /// <param name="position">Where the bubble appears</param>
        /// <param name="velocity">The trajectory of the bubble</param>
        public void SpawnBubble(Vector2 position, Vector2 velocity)
        {
            for (int j = 0; j < bubbles.Length; j++)
            {
                // find a bubble that is not visible
                if (bubbles[j].Visibility == Visibility.Collapsed)
                {
                    // make it visible
                    bubbles[j].Visibility = Visibility.Visible;
                    bubbles[j].Opacity = 1;
                    
                    // set its velocity
                    bubbleMovement[j] = velocity;
                    
                    // Generate a random size for the bubble
                    double size = randomizer.NextDouble() * 5.0;
                    bubbles[j].Width = bubbles[j].Height = size;
                    
                    // Set the bubble's position (centering it by substracting half the size of the bubble)
                    Canvas.SetLeft(bubbles[j], position.X - size / 2.0);
                    Canvas.SetTop(bubbles[j], position.Y - size / 2.0);
                    break;
                }
            }
        }


        /// <summary>
        /// Update visible bubbles 
        /// </summary>
        /// <param name="elapsed">Time factor to apply to movement</param>
        public void Update(TimeSpan elapsed)
        {
            for (int i = 0; i < bubbles.Length; i++)
            {
                if (bubbles[i].Visibility == Visibility.Visible)
                {
                    // get bubble's position
                    double x = Canvas.GetLeft(bubbles[i]);
                    double y = Canvas.GetTop(bubbles[i]);

                    // move it by our velocity times the elapsed amount of time
                    Canvas.SetLeft(bubbles[i], x + elapsed.TotalSeconds * bubbleMovement[i].X);
                    Canvas.SetTop(bubbles[i], y + elapsed.TotalSeconds * bubbleMovement[i].Y);
                    
                    // it takes 1 second to fade out a bubble.
                    bubbles[i].Opacity -= elapsed.TotalSeconds;

                    // if the bubble is completely faded out, we make it invisible (disabling it)
                    if (bubbles[i].Opacity <= 0)
                    {
                        bubbles[i].Visibility = Visibility.Collapsed;
                    }

                }
            }
        }

    }


}
